package com.hyc.myvideo.ui.base;


import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.acmenxd.toaster.Toaster;
import com.hyc.myvideo.AppContext;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.api.Api;
import com.hyc.myvideo.data.api.model.UserInfo;
import com.hyc.myvideo.ui.service.AudioService;
import com.hyc.network.exception.ApiException;
import com.hyc.network.listener.HttpOnNextListener;
import com.hyc.utils.AppDavikActivityMgr;
import com.hyc.utils.StringUtils;
import com.umeng.socialize.UMShareAPI;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import timber.log.Timber;

/***
 * FragmentActivity
 * 基类FragmentActivity
 */
public abstract class BaseFActivity extends FragmentActivity implements HttpOnNextListener {
    protected AppDavikActivityMgr appDavikActivityMgr;
    public Resources res;/*获取资源*/
    public DisplayMetrics displaysMetrics;
    public abstract int getContentViewId();
    protected  abstract void initView();
    protected  abstract void initData();
    protected  abstract void oNext(String resulte, String method);
    protected  abstract void oError();
    private Unbinder unbind;
    public Api api = null;
    /**全局应用*/
    protected AppContext appContext;
    public static UserInfo userInfo;
    public AudioService audioService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId());
        unbind = ButterKnife.bind(this);
        res = getResources();
        this.appDavikActivityMgr = AppDavikActivityMgr.getScreenManager();
        displaysMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaysMetrics);
        api = new Api(this,this);
        this.appContext = AppContext.get(this);
        userInfo = appContext.getUserInfo();
        audioService=appContext.getAudioService();
        initView();
        initData();
    }
    @Override
    protected void onResume() {
        super.onResume();
        userInfo = appContext.getUserInfo();
        this.appDavikActivityMgr.addActivity(this);
    }
    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //释放所有绑定的view
        unbind.unbind();
        api = null;
        UMShareAPI.get(this).release();
        this.appDavikActivityMgr.removeActivity(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode,resultCode,data);
    }

    /***
     * 设置head 返回事件
     * @param isVISIBLE 是否显示
     * 显示并启用 true  false 隐藏返回
     */
    public void setLeftBtnClick(boolean isVISIBLE,String str) {
        ImageView ImageLeft = (ImageView) findViewById(R.id.head_left_img);
        if (isVISIBLE) {
            ImageLeft.setVisibility(View.VISIBLE);
        }
        TextView tv = (TextView) findViewById(R.id.head_center_title);
        if (tv != null) {
            tv.setText(str);
            tv.setVisibility(View.VISIBLE);
        }
        if (ImageLeft != null) {
            ImageLeft.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    /***
     * 文本按钮
     * @param TitleContent 文本内容
     * @param clickListener 添加点击事件
     * @param clickable 是否展示
     */
    public void setTextViewClickListener(String TitleContent, View.OnClickListener clickListener, boolean clickable) {
        if (StringUtils.isEmpty(TitleContent)) {
            return;
        }
        TextView tv = (TextView) findViewById(R.id.head_right_title);
        if (tv != null) {
            tv.setText(TitleContent);
            tv.setVisibility(View.VISIBLE);
            tv.setClickable(clickable);
            tv.setOnClickListener(clickListener);
        }
    }

    /**
     * 右边图标事件
     */
    public void setRightClick(int img, View.OnClickListener clickListener) {
        ImageView imageView = (ImageView) findViewById(R.id.head_right_img);
        imageView.setVisibility(View.GONE);
        if (img == 0) return;
        imageView.setVisibility(View.VISIBLE);
        imageView.setImageResource(img);
        if (clickListener != null) {
            imageView.setOnClickListener(clickListener);
        }
    }


    /**
     * 从资源获取字符串
     */
    public String getStr(int resId) {
        return res.getString(resId);
    }

    /**
     * 从EditText 获取字符
     */
    public String getBEDstr(EditText editText) {
        return editText.getText().toString().replaceAll(" ", "").trim();
    }

    /**设置颜色*/
    public int setBColor(@ColorRes int id){
       return ContextCompat.getColor(this,id);
    }


    /**
     * TextView 获取字符
     */
    public String getText(TextView textView) {
        return textView.getText().toString().replaceAll(" ", "").trim();
    }

    @Override
    public void onNext(String resulte, String method) {
        Timber.i("============onNext=================" +resulte);
        oNext(resulte,method);
    }

    @Override
    public void onError(ApiException e, String method){
        oError();
        Timber.e(e.getDisplayMessage());
        Toaster.show("无网络连接!");
    }
}
